package com.atguigu.spzx.manager.service.impl;

import com.atguigu.spzx.manager.mapper.ProductDetailsMapper;
import com.atguigu.spzx.manager.mapper.ProductMapper;
import com.atguigu.spzx.manager.mapper.ProductSkuMapper;
import com.atguigu.spzx.manager.service.ProductService;
import com.atguigu.spzx.model.dto.product.ProductDto;
import com.atguigu.spzx.model.entity.product.Product;
import com.atguigu.spzx.model.entity.product.ProductDetails;
import com.atguigu.spzx.model.entity.product.ProductSku;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @ClassName: ProductServiceImpl
 * @Package: com.atguigu.spzx.manager.service.impl
 * @Description
 * @Author 孤妤
 * @Date: 2023/12/8 16:58
 */
@Service
@Transactional
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductMapper productMapper;// 添加基本信息
    @Autowired
    private ProductSkuMapper productSkuMapper; //添加sku
    @Autowired
    private ProductDetailsMapper productDetailsMapper;// 添加图片详情列表
    @Override
    @Transactional(readOnly = true)
    public PageInfo<Product> findProductPage(Integer pageNum, Integer pageSize, ProductDto productDto) {
        PageHelper.startPage(pageNum,pageSize);
        List<Product> productList = productMapper.selectProductPage(productDto);
        return new PageInfo<>(productList);
    }

    @Override
    public void addProduct(Product product) {
        // 添加product基本信息
        productMapper.insertProduct(product);
        // 添加sku信息
        List<ProductSku> productSkuList = product.getProductSkuList();
        for(int i=0,size=productSkuList.size();i<size;i++){
            // 获取ProductSku对象
            ProductSku productSku = productSkuList.get(i);
            // 设置skuCode
            productSku.setSkuCode(product.getId()+"_"+i);
            // 设置skuName
            productSku.setSkuName(product.getName()+productSku.getSkuSpec());
            // 设置productId
            productSku.setProductId(product.getId());
            // 设置销量
            productSku.setSaleNum(0);
            // 设置状态
            productSku.setStatus(0);
        }
        productSkuMapper.insertProductSku(productSkuList);
        // 添加图片详情信息.获取productDetails对象并赋值
        ProductDetails productDetails = new ProductDetails();
        productDetails.setProductId(product.getId());
        productDetails.setImageUrls(product.getDetailsImageUrls());
        productDetailsMapper.insertProductDetails(productDetails);
    }

    @Override
    @Transactional(readOnly = true)
    public Product findProductById(Long id) {
        // 查询商品管理基本信息
        Product product = productMapper.selectProductById(id);
        // 查询商品管理sku信息
        List<ProductSku> productSkuList = productSkuMapper.selectProductSkuById(id);
        product.setProductSkuList(productSkuList);
        // 查询商品管理图片详情列表
        ProductDetails productDetails = productDetailsMapper.selectProductDetailsById(id);
        product.setDetailsImageUrls(productDetails.getImageUrls());
        return product;
    }

    @Override
    public void changeProductById(Product product) {
        // 修改基本信息
        productMapper.updateProductById(product);
        // 修改商品管理sku
        // 添加sku信息
        List<ProductSku> productSkuList = product.getProductSkuList();
       // 设置skuName
        productSkuList.forEach(item->{
            item.setSkuName(product.getName()+item.getSkuSpec());
        });
        productSkuMapper.updateProductSkuById(productSkuList);
        // 修改商品管理图片列表详情
        ProductDetails productDetails = new ProductDetails();
        productDetails.setImageUrls(product.getDetailsImageUrls());
        productDetails.setProductId(product.getId());
        productDetailsMapper.updateProductDetailsById(productDetails);
    }

    @Override
    public void removeProductById(Long id) {
        // 删除商品管理基本信息
        productMapper.deleteProductById(id);
        // 删除商品管理sku信息
        productSkuMapper.deleteProductSkuById(id);
        // 删除商品图片详情信息
        productDetailsMapper.deleteProductDetailsById(id);
    }

    @Override
    public Product changeProductStatusById(Long id, Integer status) {
        Product product = new Product();
        if (status==1){
            product.setAuditMessage("上架成功");
        }else {
            product.setAuditMessage("下架成功");
        }
        // 将参数传入map中传参
        Map<String, Object> map = new HashMap<>();
        map.put("id",id);
        map.put("status",status);
        productMapper.updateProductStatusById(map);
        return product;
    }

    @Override
    public Product changeProductAuditStatusById(Long id, Integer auditStatus) {
        // 将参数传入map中传参
        Product product = new Product();
        Map<String, Object> map = new HashMap<>();
        map.put("id",id);
        map.put("auditStatus",auditStatus);
        if (auditStatus==-1){
            map.put("auditMessage","审批驳回");
            product.setAuditMessage("审批驳回");
        }else {
            map.put("auditMessage","审批通过");
            product.setAuditMessage("审批通过");
        }
        productMapper.updateProductAuditStatusById(map);
        return product;
    }
}